$(function() {
    loadData('', '', '', 15, 0);
    setPageEvent();
})

function setPageEvent() {
    $("a.page-num").click(function() {
        var number = $(this).text();
        loadData('', '', '', 15, (number - 1));
        $("a.page-num").removeClass("active");
        $(this).addClass("active");
    });
}

function loadData(code, businessName, stateNow, limit, offset) {
    $("#eforms-content").html("");
    $.ajax({
        url: "/eform/list",
        data: {
            code: code,
            businessName: businessName,
            stateNow: stateNow,
            limit: limit,
            offset: offset*15,
            time: new Date()
        },
        dataType: "json",
        method: "get",
        success: function(result) {
            var dataList = result.rows;
            var len = (dataList.length);
            var html = "";
            for (var j = 0; j < len; j++) {
                var res = dataList[j];
                html = html + "<tr id='" + res.id + "'>";
                html = html + "<td>" + res.code + "</td>" + "<td>" +
                    res.businessType + "</td>" + "<td title='" +
                    res.businessName + "'>" +
                    setMoreTextHtml(res.businessName, 10) + "</td>"
                    // + "<td>"+res.budgetCode+"</td>"
                    +
                    "<td>" + res.proposer + "</td>" + "<td>" + res.dept +
                    "</td>" + "<td>" + res.proposer + "</td>" +
                    "<td title='" + res.auditors + "'>" +
                    setMoreTextHtml(res.auditors, 7) + "</td>" +
                    "<td title='" + res.stateNow + "'>" +
                    setMoreTextHtml(res.stateNow, 5) + "</td>"; +
                "</tr>";
            }
            $("#eforms-content").html(html);
            var total = result.total;
            var n_ = total % limit; //余数
            var n = total / limit;
            n = (n_>0) ? n + 1 : n;
            var pages = getPageGrid(offset, n);
            for (var t = pages.length; t < 5; t++) {
                var pre = pages[t-1];
                if (pre < n) {
                    pages[t] = pre + 1;
                }
            }
			var pagehtml = "<li><a aria-label='Previous'> <span aria-hidden='true'>&laquo;</span></a></li>";
			for(var i=0;i<pages.length;i++) {
				pagehtml = pagehtml + "<li><a href='#' class='page-num";
				if(offset == i) {
					pagehtml = pagehtml + " active";
				}
				pagehtml = pagehtml + "'>"+pages[i]+"</a></li>";
			}
			pagehtml = pagehtml + "<li><a aria-label='Next'> <span aria-hidden='true'>&raquo;</span></a></li>";
			$("#page-box").html(pagehtml);
			initEvent();
			setPageEvent();
        }
    });
}

/**
 * 
 * @param offset：当前页（从0开始）
 * @param n：总数
 * @returns
 */
function getPageGrid(offset, n) {
	var pages = new Array();
	if (offset > 2) { //三页以外
        if(offset+1 == n) {
        	for(var i=0,t=offset,j=3;t>offset-4;t--,i++,j--) {
        		pages[j] = offset-i;
        	}
        } else {
        	pages[2] = offset;
            pages[1] = offset - 1;
            pages[0] = offset - 2;
        }
    } else {
        for (var i = 0; i <= offset; i++) {
            pages[i] = i+1;
        }
    }
	for (var t = pages.length; t < 5; t++) {
        var pre = pages[t-1];
        if (pre < n) {
            pages[t] = pre + 1;
        }
    }
	return pages;
}

function initEvent() {
    $('#eforms-content tr').click(function() {
        var id = $(this).attr("id");
        window.open('/statistics/eform/' + id);
    });
}

function setMoreTextHtml(v, len) {
    if (v == null && v == '') {
        return '';
    }
    var l1 = false;
    if (v.length > len) {
        l1 = true;
        v = v.substring(0, len - 1);
        return (v + '...');
    }
    return v;
}

function findIframeByUri(olduri, newuri) {
    var iframes = parent.document.getElementsByTagName("iframe");
    console.info(iframes.length);
    if (null != iframes && iframes.length > 0) {
        for (var i = 0; i < iframes.length; i++) {
            var iframe = $(iframes[i]);
            console.info(iframe.attr("src"));
            if (iframe.attr("src") === olduri) {
                var src = iframe.attr("src", newuri);
            }
        }

    }
}